<?php
set_include_path("../include;include");
require_once "/util/helperFunctions.php";
require_once "/dao/GroupDAO.php";

class Group {
	private $id;
	private $name;

	private static $dao = null; // The GroupDAO initialised by initDAO

	function __construct() {
	}

	public static function createNew($name) {
		$instance = new self();
		$instance->name = $name;
		return $instance;
	}

	public function save() {
		$group_id = self::$dao->save($this);
		$this->id = $group_id;
		return $group_id;
	}
	
	/**
	 * Static save method, to save a Group object from somwhere else;
	 * @param Group $group The group that is to be saved
	 */
	public static function saveGroup(Group $group) {
		$group_id = self::$dao->save($group);
		$group->setId($group_id);
	}
	
	/**
	 * Static load method, to load an existing Group object from somewhere else, using its id.
	 * @param integer $id The unique id of the Group
	 */
	public static function loadGroup(integer $id) {
		$group = new self();
		$group->id = $id;
        $group->load($id);
		return $group;
	}

	public function load(integer $id) {
		$this->id = $id;
		$this->name = self::$dao->getNameByGroupId($id);
	}

	public function getId() {
		return $this->id;
	}

	public function setId(integer $id) {
		$this->id = $id;
	}

	public function getName() {
		return $this->name;
	}

	public function setName(string $name) {
		$this->name = $name;
	}
	
	
	public function __toString() {
		return (string) $this->getName();
	}
	

	public static function InitDAO() {
		self::$dao = GroupDAO::getInstance();
	}
} Group::InitDAO();